<?php

	/*
	* file: pageStructUIMod.php
	*
	* contents:	provides functions to start and manage a mysql database interaction.
	*
	* author: Francesco Rubert, Federico Zanco (minor revs)
	*/



	/*
	* function connect()
	*
	* Input required:	none
	*
	* Output:	return the connection ID. Show login page with the error otherwise
	*
	* Author: Francesco Rubert, Federico Zanco (minor revs)
	*/

	function connect() {
		
		//try to connect to localhost mysql server with username and password posted by user
		$con = @mysql_connect('localhost', $_SESSION['username'], $_SESSION['password']);

		if ($con) {
			//if connected try to select DoGe database
			if (@mysql_select_db('DoGe', $con) === false) {

				//if can't select DoGe DB unset session values, show login page, notify the error
				session_unset();
				showLogin('Connessione con il server riuscita ma la selezione della base di dati &egrave; fallita. Contattare l\'amministratore di sistema');
			}
		} else {
			//if can't connect unset session values, show login page, notify the error
			session_unset();
			showLogin('Connessione con il server della base di dati non riuscita. Contattare l\'amministratore di sistema');
		}

		return $con;
	}



	/*
	* function disconnect()
	*
	* Input required:	$con: connection ID
	*
	* Output:	close connection from mysql server. Just a wrapper...
	*
	* Author: Francesco Rubert
	*/

	function disconnect($con) {
		mysql_close($con);
	}



	/*
	* function query($query,$con)
	*
	* Input required:	$query: the query to process
	*					$con: connection ID
	*
	* Output:	process a query and return results or show an error page
	*
	* Author: Francesco Rubert , Federico Zanco (minor revs)
	*/

	function query($query, $con) {
		//exec the query
		$result = @mysql_query($query, $con);

		//if the query get an error, show an error page
		if (!$result) {
			showErrorPage('La query ha generato un errore.'); // . mysql_error());
		} else
			return $result;
	}
	
?>
